/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.util.web;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by walle on 12/9/15.
 */
public class FormatUnits {
    public static String getFileLengthAndUnit(int fileLength) {
        String unit = "b";
        Integer length = Integer.valueOf(fileLength);
        if (Integer.valueOf(fileLength).intValue() / 1024 > 0) {
            length = Integer.valueOf(Integer.valueOf(fileLength).intValue() / 1024);
            unit = "kb";
        }
        if (Integer.valueOf(fileLength).intValue() / 1024 / 1024 > 0) {
            length = Integer.valueOf(Integer.valueOf(fileLength).intValue() / 1024 / 1024);
            unit = "mb";
        }
        return length + unit;
    }

    public static String getReFormateTime14(String timeStr) {
        timeStr = timeStr.replaceAll("-", "").replaceAll(":", "").replaceAll(" ", "") + "00000000000000";

        return timeStr.substring(0, 14);
    }

    public static String getFormateTime(String timeStr08_14, boolean isshowCurrentDay) {
        if ((timeStr08_14 == null) || (timeStr08_14.trim().length() <= 0)) {
            return null;
        }
        int tlength = timeStr08_14.length();
        timeStr08_14 = timeStr08_14 + "00000000";
        String yyyy = timeStr08_14.substring(0, 4);
        String MM = timeStr08_14.substring(4, 6);
        String dd = timeStr08_14.substring(6, 8);
        String HH = timeStr08_14.substring(8, 10);
        String mm = timeStr08_14.substring(10, 12);
        String ss = timeStr08_14.substring(12, 14);
        String returnData = null;
        if ((tlength == 8) && (returnData == null)) {
            returnData = yyyy + "-" + MM + "-" + dd;
        }
        if ((tlength == 10) && (returnData == null)) {
            returnData = yyyy + "-" + MM + "-" + dd + " " + HH;
        }
        if ((tlength == 12) && (returnData == null)) {
            returnData = yyyy + "-" + MM + "-" + dd + " " + HH + ":" + mm;
        }
        if (returnData == null) {
            returnData = yyyy + "-" + MM + "-" + dd + " " + HH + ":" + mm + ":" + ss;
        }

        SimpleDateFormat _sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currentday = _sdf.format(new Date());
        return isshowCurrentDay ? returnData.replace(currentday, "今天") : returnData;
    }

    public static String reFormateTime(String timeStr, int length) {
        if ((timeStr == null) || (timeStr.trim().length() <= 0)) {
            return null;
        }
        timeStr = timeStr.replace(":", "").replace("-", "").replace(" ", "");
        if (timeStr.length() > length) {
            timeStr = timeStr.substring(0, length);
        }
        return timeStr;
    }
}
